import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/Login'
import home from '../pages/home'
import welcome from '../pages/welcome'
// 导入用户管理模块
import user from '../pages/users/user'
// 导入权限管理模块
import rights from '../pages/power/Rights'
// 导入用户规则权限组件
import roles from '../pages/power/roles'

// 导入商品分类组件
import Categories from '../pages/goods/Categories'
import Params from '../pages/goods/Params'
Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    // 通过路由重定向,将页面重新跳转到/login
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    {
      path: '/home',
      component: home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: welcome },
        { path: '/users', component: user },
        { path: '/rights', component: rights },
        { path: '/roles', component: roles },
        { path: '/categories', component: Categories },
        { path: '/params', component: Params }
      ]
    }
  ]
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from 代表从哪个路径跳转而来
  // next表示放行
  // next()直接放行  next('/login') 强制跳转
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) next('/login')
  next()
})
export default router
